/**
 * Copyright (c) 2022 Beijing Jiaotong University
 * PhotLab is licensed under [Open Source License].
 * You can use this software according to the terms and conditions of the [Open Source License].
 * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *
 * See the [Open Source License] for more details.
 *
 * Author: Zhenjie Wei
 * Created: Mar. 13, 2023
 * Supported by: National Key Research and Development Program of China
 */

import { Disposable } from "@/common/disposable"
import { libraryService as libraryAPI } from "./client"

enum CompoLibNodeType {
    COMPONENT,
    DIRECTORY
}

interface ICompoLibNode {
    name: string
    path: string
    type: CompoLibNodeType
    // content: PhotComponent | ICompoLibNode[] | null
}

export class CompoLibrary extends Disposable {
    private _root: ICompoLibNode = {
        name: "root",
        path: "./",
        type: CompoLibNodeType.DIRECTORY,
        // content: null
    }
    private _currentDir = this._root

    private async requestFileList(path: string): Promise<ICompoLibNode[]> {
        return []
    }

    public changeDirectory(path: string) {
        return
    }

    public getCurrentList(): ICompoLibNode[] {
        // return this._currentDir.content as ICompoLibNode[]
        return []
    }

    constructor() {
        super()
        return
    }
}
export const libraryManager = new class LibraryManager extends Disposable {
    private _libraryList: any[] = []
    private _allList: any[] = []

    constructor() {
        super()
        return
    }

    public get libraryList(): any[] {
        return this._libraryList
    }

    public get allLibraryList(): any[] {
        return this._allList
    }

    public async getLibraryList() {
        let allList: any = []
        let libraryList: any = []
        const cateList: any = await libraryAPI.getLibraryList("./")
            // console.log(res)
        for (let i = 0; i < cateList.length; i++) {
            let componentList: any = []
            const cate = cateList[i]
            const libraryItem: any = {
                id: cate.name,
                label: cate.name,
                value: cate.name,
                path: cate.path,
                bg: true,
                children: []
            }
            const compoList: any = await libraryAPI.getLibraryList('./' + cate.path)
            compoList.map((compo: any) => {
                if (compo.type === 1) {
                    componentList.push({
                        id: compo.name,
                        label: compo.name.replace(/.phtc/, ''),
                        value: compo.name.replace(/.phtc/, ''),
                        description: compo.description,
                        component: compo
                    })
                }
            })
            // console.log(componentList)
            libraryItem.children = componentList
            allList = allList.concat(...componentList)
            libraryList.push(libraryItem)
        }
        this._allList = allList
        this._libraryList = libraryList
        
        return {
            allList,
            libraryList
        }
    }

    public async searchComponent(queryString: string) {
        const libraryList = await libraryAPI.searchComponent(queryString)
        return libraryList
    }
    public async getComponentPrototype(path: string) {
        return await libraryAPI.getComponentPrototype(path)
    }

    public activate() {
        return
    }
}